package com.hsource.doctorcenter.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hsource.common.utils.CommonResp;
import com.hsource.common.utils.DateUtil;
import com.hsource.common.utils.resultUtils.controller.BaseController;
import com.hsource.doctorcenter.domain.healthRecords.HealthRecords;
import com.hsource.doctorcenter.service.healthRecords.IHealthRecordsService;
import com.hsource.doctorcenter.service.problem.ProblemService;
import com.hsource.doctorcenter.service.user.UserService;
import com.hsource.doctorcenter.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/9/23 9:15
 * @describe 用户控制层
 */
@Controller
@RequestMapping("/user")
public class UserController  extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private IHealthRecordsService healthRecordsService;
    @Autowired
    private ProblemService problemService;

    @PostMapping("/userLogin")
    @ResponseBody
    public CommonResp searchDoctors(String userId, String password) throws ParseException, NoSuchAlgorithmException {
        String s = userService.userLogin(userId, password);
        JSONObject jsonObject = JSONObject.parseObject(s);
        if(jsonObject.get("error").toString().equals("0")){
            return success();
        }
        return failed(jsonObject.get("error_msg").toString());
    }

    @PostMapping("/orderAndHealth")
    @ResponseBody
    public CommonResp orderAndHealth(String userId) throws ParseException, NoSuchAlgorithmException {
        HealthRecords healthRecords=new HealthRecords();
        healthRecords.setUserId(userId);
        List<HealthRecords> healthRecordsList = healthRecordsService.selectHealthRecordsListByUserId(healthRecords);
        healthRecordsList.stream().forEach(a->{
            a.setBirthdayDate(DateUtil.getStrFromDate( a.getBirthday(),"yyyy-MM-dd"));
        });
        Map<String,Object> objectMap=new HashMap<>();
        objectMap.put("healthRecordsList",healthRecordsList);


        Map<String, Object> historicalQuestions = problemService.getHistoricalQuestions(userId, 0, 2, "imagetext");
        if(StringUtils.isNull(historicalQuestions)||historicalQuestions.size()<=0){
            historicalQuestions = problemService.getHistoricalQuestions(userId, 0, 2, "phone");
        }
        objectMap.putAll(historicalQuestions);
        objectMap.put("userId",userId);
        return success(objectMap);
    }


}
